package com.huw.cameramanagers;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.shape.Shape;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.light.gamecomponents.IGameComponents;
import com.light.roller.Player;
import com.light.roller.ResourceManager;
import com.light.roller.Roller;

public class CameraGhost implements IGameComponents{
	
	private Roller m_game;
	private Player m_player;
	private Body m_body;
	private Sprite m_sprite;
	private String m_resourceName = "Black";
	private TextureRegion m_textureRegion;
	private PhysicsConnector m_PhysicsConnector;
	public static enum FollowStyle {COMPUTER,ANDROID};
	private final FollowStyle m_style;
	
	private Vector2 velocity;
	private Vector2 m_InitialPosition;
	
	
	public CameraGhost(Roller game,Player player,FollowStyle style)
	{
		m_game = game;
		m_player = player;
		velocity = new Vector2(0,0);
		m_style = style;
		m_InitialPosition = new Vector2(0,0);
	}
	
	public CameraGhost(Roller game,Player player,FollowStyle style,Vector2 InitialPosition)
	{
		m_game = game;
		m_player = player;
		velocity = new Vector2(0,0);
		m_style = style;
		m_InitialPosition = new Vector2(InitialPosition);
	}
	
	@Override
	public void onLoadResources()
	{
		ResourceManager.onLoadResources(m_resourceName, m_game);
		m_textureRegion = ResourceManager.getTextureRegion(m_resourceName);
		
	}
	
	@Override
	public void unLoadResources() {
		m_textureRegion = null;
		ResourceManager.unLoadResources(m_resourceName, m_game);
	}
	
	@Override
	public void onLoadEngine() {
		// TODO Auto-generated method stub
		
	}

	Scene m_scene;
	
	@Override
	public void onLoadScene(Scene scene, PhysicsWorld physicsWorld)
	{
		FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.2f, 0.0f);
		objectFixtureDef.filter.categoryBits = Roller.CATEGORYBIT_CAMERA;
		objectFixtureDef.filter.maskBits = Roller.MASKBIT_CAMERA;
	
		m_scene = scene;
		
		m_sprite = new Sprite(0,
				 						0,
				                      m_textureRegion);				
		
		m_sprite.setSize(Roller.CAMERA_WIDTH, Roller.CAMERA_HEIGHT);	
		
		setCenter(m_InitialPosition.x-Roller.CAMERA_WIDTH/2,m_InitialPosition.y-Roller.CAMERA_HEIGHT/2);
		
		m_sprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		m_sprite.setAlpha(1.0f);
		
		scene.attachChild(m_sprite);
		
		if(m_style==FollowStyle.ANDROID)
		{	
			m_body = PhysicsFactory.createBoxBody(physicsWorld, 
												  m_sprite, 
												  BodyType.KinematicBody, 
												  objectFixtureDef);
			m_PhysicsConnector = new PhysicsConnector(m_sprite, m_body, true, false);
			physicsWorld.registerPhysicsConnector(m_PhysicsConnector);
		}
	}
	
	public void LightOnCamera()
	{
		
		
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(0.5f,1,0)));
		m_sprite.registerEntityModifier(EntityModifier);
	}
	
	public void LightOffCamera()
	{
		m_scene.detachChild(m_sprite);
		m_scene.attachChild(m_sprite);		
		
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(0.5f,0,1)));
		m_sprite.registerEntityModifier(EntityModifier);
	}
	
	public void LightOffCamera(float time)
	{
		m_scene.detachChild(m_sprite);
		m_scene.attachChild(m_sprite);		
		
		LoopEntityModifier EntityModifier = new LoopEntityModifier(0, new SequenceEntityModifier(new AlphaModifier(time,0,1)));
		m_sprite.registerEntityModifier(EntityModifier);
	}
	
	public boolean isLightOn()
	{
		boolean result = false;
		if(m_sprite.getAlpha()==0.0f)result=true;
		return result;
	}
	
	public boolean isLightOff()
	{
		boolean result = false;
		if(m_sprite.getAlpha()==1.0f)result=true;
		return result;
	}
	
	@Override
	public void removeFromScene(Scene scene, PhysicsWorld physicsWorld) 
	{
		//scene.detachChild(m_sprite);
		physicsWorld.unregisterPhysicsConnector(m_PhysicsConnector);
		physicsWorld.destroyBody(m_body);
	
		m_PhysicsConnector = null;
		m_body = null;
		m_sprite = null;
	}
	
	
	public void setLinearVelocity(Vector2 v)
	{	
		velocity = v;
	}
	
	public void onUpdate() 
	{
		boolean xtouched = false;
		boolean ytouched = false;
		
		double ax1 ;
		double ax2 ;
		double ay1 ;
		double ay2 ;
		
		double preax1 = m_sprite.getX();
		double preax2 = m_sprite.getX()+m_sprite.getWidth();
		double preay1 = m_sprite.getY();
		double preay2 = m_sprite.getY()+m_sprite.getHeight();
		
		ax1 = m_sprite.getX()+velocity.x*4+this.m_player.getBody().getLinearVelocity().x*0.75;;
		ax2 = m_sprite.getX()+m_sprite.getWidth()+velocity.x*4+this.m_player.getBody().getLinearVelocity().x*0.75;
		ay1 = m_sprite.getY()+velocity.y*4+this.m_player.getBody().getLinearVelocity().y*0.75;
		ay2 = m_sprite.getY()+m_sprite.getHeight()+velocity.y*4+this.m_player.getBody().getLinearVelocity().y*0.75;;				
	
		for(int i = 0; i<CameraStoper.cameraStoperList.size();i++)
		{
			Shape tempS = CameraStoper.cameraStoperList.get(i).getShape();
			double bx1 = tempS.getX();
			double bx2 = tempS.getX() + tempS.getWidth();
			double by1 = tempS.getY();
			double by2 = tempS.getY() + tempS.getHeight();
			
			if(ax1<bx2&&ax2>bx1&&ay1<by2&&ay2>by1)
			{
				if(ax1<bx2&&ax2>bx1&&!(preax1<bx2&&preax2>bx1))xtouched = true;
				if(ay1<by2&&ay2>by1&&!(preay1<by2&&preay2>by1))ytouched = true;
			}
		}
		

		if(ytouched)this.velocity.y=0;
		if(xtouched)this.velocity.x=0;
		
		if(m_style==FollowStyle.ANDROID)
		{
		velocity.x*=2;
		velocity.y*=2;
		velocity.x+=this.m_player.getBody().getLinearVelocity().x*0.75;
		velocity.y+=this.m_player.getBody().getLinearVelocity().y*0.75;
		
		if(ytouched)this.velocity.y=0;
		if(xtouched)this.velocity.x=0;
		
		m_body.setLinearVelocity(velocity);	
		}
		else if(m_style==FollowStyle.COMPUTER)
		{
			Vector2 temp = getCenter();
			setCenter(temp.x+velocity.x,temp.y+velocity.y);
		}
		
		this.velocity.x = 0;
		this.velocity.y = 0;
	}
	
	public void setCenter(float x,float y)
	{
	    m_sprite.setPosition(x-m_sprite.getWidth()/2, y-m_sprite.getHeight()/2);
	}
	
	public Vector2 getCenter()
	{
		Vector2 temp = new Vector2();
		temp.x = m_sprite.getX()+m_sprite.getWidth()/2;
		temp.y = m_sprite.getY()+m_sprite.getHeight()/2;
		return temp;
	}

	@Override
	public void rotate(float angle) {
		// TODO Auto-generated method stub
		
	}
	
	
}
